package com.aidex.web.controller.biz;

import com.aidex.biz.domain.ProductInfo;
import com.aidex.biz.domain.ProductSku;
import com.aidex.biz.service.ProductInfoService;
import com.aidex.biz.service.ProductSkuService;
import com.aidex.biz.service.RubbishTypeService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

/**
 * 产品信息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-06-13
 */
@RestController
@RequestMapping("/biz/bizProductInfo")
public class ProductInfoController extends BaseController {
    @Autowired
    private ProductInfoService productInfoService;
    @Autowired
    private ProductSkuService productSkuService;
    @Autowired
    private RubbishTypeService rubbishTypeService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:list')")
    @GetMapping("/list")
    public R<PageInfo> list(ProductInfo productInfo, HttpServletRequest request, HttpServletResponse response) {
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            productInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        productInfo.setPage(new PageDomain(request, response));
        return R.data(productInfoService.findPage(productInfo));
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:query')")
    @GetMapping(value = "/{id}")
    public R<ProductInfo> detail(@PathVariable("id") String id) {
        return R.data(productInfoService.get(id));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated ProductInfo productInfo) {

        productInfo.setId(UUID.randomUUID().toString());
        productInfo.setIsNewRecord();
        boolean proIsOk = productInfoService.save(productInfo);
        List<ProductSku> skus = productInfo.getProductSkuList();
        boolean proSkuOk = false;
        for (int i = 0; i < skus.size(); i++) {
            ProductSku sku = skus.get(i);
            sku.setProductId(productInfo.getId());
            sku.setStatus("0");
            sku.setSort(i);
            proSkuOk = productSkuService.save(sku);
        }
        return R.status(proIsOk && proSkuOk);
    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ProductInfo productInfo) {
        boolean proIsOk = productInfoService.save(productInfo);
        boolean proSkuOk = false;
        List<ProductSku> skus = productInfo.getProductSkuList();
        for (int i = 0; i < skus.size(); i++) {
            ProductSku sku = skus.get(i);
            proSkuOk = productSkuService.save(sku);
        }
        return R.status(proIsOk && proSkuOk);
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:edit')")
    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PutMapping("/updateShowIndexAndStatus")
    public R updateShowIndexAndStatus(@RequestBody @Validated ProductInfo productInfo) {
        return R.status(productInfoService.updateShowIndexAndStatus(productInfo));
    }


    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(productInfoService.deleteProductInfoByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(productInfoService.findMaxSort(new ProductInfo()));
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizProductInfo:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(ProductInfo productInfo) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            productInfo.setCompanyId(null);
        } else {
            productInfo.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<ProductInfo> list = productInfoService.findList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        return util.exportExcel(list, "产品信息数据");
    }

}
